﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace SpWiki.Lib.Extensions
{
    public static class SpWebExtensions
    {
        /// <summary>
        /// Does unsafe updates.
        /// </summary>
        /// <param name="web">The web.</param>
        /// <param name="action">The action.</param>
        public static void DoUnsafeUpdate(this SPWeb web, Action action)
        {
            var allowUnsafeUpdates = false;
            try
            {
                allowUnsafeUpdates = web.AllowUnsafeUpdates;
                web.AllowUnsafeUpdates = true;
                action();
            }
            finally
            {
                web.AllowUnsafeUpdates = allowUnsafeUpdates;
            }
        }

        /// <summary>
        /// Gets the list by title.
        /// </summary>
        /// <param name="web">The web.</param>
        /// <param name="listTitel">The list titel.</param>
        /// <returns></returns>
        public static SPList GetListByTitle(this SPWeb web, string listTitel)
        {
            SPList list = null;
            try
            {
                // init
                if (web == null) { throw new Exception("SPWeb passed is null"); }
                if (string.IsNullOrEmpty(listTitel)) { throw new Exception("List title passed is invalid."); }
                listTitel = listTitel.Trim();

                if (web.Lists.Count > 0)
                {
                    list = web.Lists.TryGetList(listTitel);
                }
            }
            catch (Exception ex)
            {
                //Log.WriteError("8t7otu8ifytfrtr76t", ex); todo logging
                list = null;
            }
            return list;
        }


        /// <summary>
        /// Gets the list template by internal template name.
        /// </summary>
        /// <param name="web">The web.</param>
        /// <param name="listTemplateInteralName"> The template list interal name.</param>
        /// <returns></returns>
        public static SPListTemplate GetListTemplateByInternalName(this SPWeb web, string listTemplateInteralName)
        {
            SPListTemplate result = null;
            try
            {
                foreach (SPListTemplate listTemplate in web.ListTemplates)
                {
                    if (listTemplate.InternalName.ToLower().Trim() != listTemplateInteralName.ToLower().Trim()) continue;
                    result = listTemplate;
                    break;
                }
            }
            catch { return result; }
            return result;
        }

        /// <summary>
        /// Gets the list template id by internal template name.
        /// </summary>
        /// <param name="web">The web.</param>
        /// <param name="listTemplateInteralName"> The template list interal name.</param>
        /// <returns></returns>
        public static string GetListTemplateIdByInternalName(this SPWeb web, string listTemplateInteralName)
        {
            return web.GetListTemplateByInternalName(listTemplateInteralName).Type_Client.ToString(CultureInfo.InvariantCulture);
        }

        /// <summary>
        /// Gets a list by URL.
        /// </summary>
        /// <param name="web">The web.</param>
        /// <param name="url">The URL.</param>
        /// <returns></returns>
        public static SPList GetListByUrl(this SPWeb web, string url)
        {
            SPList list = null;
            try
            {
                return web.GetList(url);
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public static Guid GetListIdByUrl(this SPWeb web, string url)
        {
                var list = web.GetList(url);
                return list.ID;
        }

    }
}
